package com.yangwp.java.spider.weibo.src;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.yangwp.java.spider.weibo.model.User;
import com.yangwp.java.spider.weibo.service.HtmlService;
import com.yangwp.java.spider.weibo.service.UserService;

/**
 * @author 11070538
 */
@Component("WeiboUserInfo")
public class WeiboUserInfo {

    @Autowired
    private HtmlService htmlService;
    @Autowired
    private UserService userService;

    public void weibo(String sub){
        //String seed = "1794454261";
        String seed = userService.getUntracked();

        int loop = 0;
        long timestamp = System.currentTimeMillis();
        int count = 0;
        while(seed != null && loop++ < 5000){
            System.out.println("track " + seed);
            for(int i=1; i<=5; i++){
                String followList = getFollowList(seed, i, sub);
                if(followList == null){
                    break;
                }
                List<User> users = analysis(followList);
//                for(User user : users){
//                    if(userService.save(user)){
//                        count ++;
//                        if(System.currentTimeMillis() - timestamp > 1000){
//                            System.out.println("insert speed:" + count + "/s");
//                            timestamp = System.currentTimeMillis();
//                            count = 0;
//                        }
//                    }
//                }
                userService.save(users);
            }
            userService.setTracked(seed);
            System.out.println("track " + seed + " over");
            seed = userService.getUntracked();
        }
    }

    private List<User> analysis(String html){

        List<User> users = Lists.newArrayList();
        Document doc = Jsoup.parseBodyFragment(html);
        Element body = doc.body();
        Elements lis = body.child(0).child(0).children();
        for(Element li : lis){
            String actionData = li.attr("action-data");
            if(actionData == null){
                continue;
            }
            List<String> ins = getIdNameSex(actionData);
            if(ins == null || ins.get(0) == null || !StringUtils.isNumeric(ins.get(0))){
                continue;
            }
            User user = new User();
            user.setUserId(ins.get(0));
            user.setUserName(ins.get(1));
            user.setSex(ins.get(2));

            Elements numbersNodes = li.getElementsByClass("conn_type");
            String follow = numbersNodes.get(0).child(0).child(0).text();
            if(follow != null && StringUtils.isNumeric(follow)){
                user.setFollow(Integer.parseInt(follow));
            }
            String fans = numbersNodes.get(1).child(0).child(0).text();
            if(fans != null && StringUtils.isNumeric(fans)){
                user.setFans(Integer.parseInt(fans));
            }
            String weibo = numbersNodes.get(2).child(0).child(0).text();
            if(weibo != null && StringUtils.isNumeric(weibo)){
                user.setWeibo(Integer.parseInt(weibo));
            }
            Elements adds = li.getElementsByClass("info_add");
            if(adds.size() > 0){
                user.setAddress(adds.get(0).child(1).text());
            }
            Elements intros = li.getElementsByClass("info_intro");
            if(intros.size() > 0){
                user.setIntro(intros.get(0).child(0).text());
            }
            Elements froms = li.getElementsByClass("info_from");
            if(froms.size() > 0){
                Elements as = froms.get(0).getElementsByTag("a");
                if(as.size() > 0){
                    user.setPath(as.get(0).text());
                }
            }
            users.add(user);
        }
        return users;
    }

    private List<String> getIdNameSex(String actionData){
        String regex = "uid=(.*?)&fnick=(.*?)&sex=(.*)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(actionData);
        if(matcher.find()){
            List<String> list = Lists.newArrayList();
            list.add(matcher.group(1));
            list.add(matcher.group(2));
            list.add(matcher.group(3));
            return list;
        }
        return null;
    }

    private String getFollowList(String id, Integer page, String cookie){
        id = "100505" + id;
        String url = "https://weibo.com/p/" + id +"/follow?page=" + page;
        String html = htmlService.getBody(url, cookie);
        if(html == null){
            return null;
        }
        final String startTag = "<div class=\\\"follow_inner\\\">";
        final String endTag = "<div class=\\\"WB_cardpage S_line1\\\" node-type=\\\"pageList\\\">";
        int start = html.indexOf(startTag);
        int end = html.indexOf(endTag, start);
        if( start <= 0 || end <= start){
            return null;
        }
        String followList = StringUtils.substring(html, start, end);
        String nFollowList = StringUtils.replace(followList, "\\", "");
        return nFollowList;
    }

}
